﻿

using DataWorks_Tools.MappModals.CalSaveModals;

namespace DataWorks_Tools.HalfHourCalSave.Common.Basic
{
    public static class Bin
    {
        /// <summary>
        /// 获取划分bin后的统计里程数据
        /// </summary>
        /// <param name="speed"></param>
        /// <param name="distance"></param>
        /// <param name="maxvalue"></param>
        /// <returns></returns>
        public static async Task< List<BinClass>> DivideBin(List<double> datalist, List<double> distance,int binvalue,int maxvalue)
        {
            List<BinClass> list = new List<BinClass>();
            await Task.Run(() =>
            {
                //这里的99没什么意义，纯粹是为了把所有大于maxvalue的都归一为99这个bin，不会重复
                var groupedData = datalist.Zip(distance, (s, d) => new { spd = s, dis = d })
                          .GroupBy(x => x.spd < maxvalue ? (int)(x.spd / binvalue) : 99, x => x.dis)
                          .Select(g => new BinClass { bin = g.Key, value = g.Sum() }).OrderBy(a=>a.bin).ToList();
                if (groupedData.Count > 0)
                {
                    list = groupedData;
                }
            });
            return list;
        }
        /// <summary>
        /// 获取划分bin后的统计时间数据
        /// </summary>
        /// <param name="speed"></param>
        /// <param name="maxvalue"></param>
        /// <returns></returns>
        public static async Task<List<BinClass>> DivideBin(List<double> datalist, int binvalue, int maxvalue)
        {
            List<BinClass> list = new List<BinClass>();
            await Task.Run(() =>
            {
                //这里的99没什么意义，纯粹是为了把所有大于maxvalue的都归一为99这个bin，不会重复
                var result = datalist.GroupBy(s => s < maxvalue ? (int)(s / binvalue) : 99)
               .Select(g => new BinClass { bin = g.Key, value = g.Count() })
               .ToList();
                if (result.Count > 0)
                {
                    list = result;
                }
            });
            return list;
        }
    }
}
